﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Лабораторная_работа_1_СОД
{
    public partial class Form2 : Form
    {
        private readonly Form1 form;
        float[,] mas,mas2;
        int n,n1 = 0;
        int m,m1 = 0;
        public Form2(Form1 form)
        {
            this.form = form;
            InitializeComponent();
            dataGridView1.VirtualMode = true;
            dataGridView2.VirtualMode = true;
            n = Convert.ToInt32(textBox1.Text);
            m = Convert.ToInt32(textBox2.Text);
            n1 = Convert.ToInt32(textBox3.Text);
            m1 = Convert.ToInt32(textBox4.Text);
            
            mas = new float[n, m];

            mas.Initialize();
            dataGridView1.RowCount = n;
            dataGridView1.ColumnCount = m;

            mas2 = new float[n1, m1];
            mas2.Initialize();
            if (form.mas1 != null)
                mas = form.mas1;
            if (form.mas2 != null)
                mas2 = form.mas2;
            dataGridView2.RowCount = n1;
            dataGridView2.ColumnCount = m1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(checkBox1.Checked)
                form.Data1Fill(mas, n, m);
            if(checkBox2.Checked)
                form.Data2Fill(mas2, n1, m1);
            this.Close();

        }

        private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            e.Value = mas[e.RowIndex, e.ColumnIndex];
        }

        private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            try
            {
                mas[e.RowIndex, e.ColumnIndex] = Convert.ToSingle(e.Value.ToString());
            }
            catch (Exception ex)
            {
                e.Value = 0;
                mas[e.RowIndex, e.ColumnIndex] = 0;
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView2_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            e.Value = mas2[e.RowIndex, e.ColumnIndex];
        }

        private void dataGridView2_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            try
            {
                mas2[e.RowIndex, e.ColumnIndex] = Convert.ToSingle(e.Value.ToString());
            }
            catch (Exception ex)
            {
                e.Value = 0;
                mas2[e.RowIndex, e.ColumnIndex] = 0;
                MessageBox.Show(ex.Message);
            }
        }



        private void button2_Click(object sender, EventArgs e)
        {
            int k;
            float[,] mas1;
            try
            {
                k = Convert.ToInt32(textBox1.Text);

             mas1 = new float[k, m];
            if (k > n)
            {
                for (int i = 0; i < n; ++i)
                    for (int j = 0; j < m; ++j)
                        mas1[i, j] = mas[i, j];
            }
            else
            {
                dataGridView1.RowCount = k;
                for (int i = 0; i < k; ++i)
                    for (int j = 0; j < m; ++j)
                        mas1[i, j] = mas[i, j]; 
            }
            n = k;
            mas = mas1;
            dataGridView1.RowCount = k;
            }
            catch (Exception ex)
            {
                k = 3;
                MessageBox.Show(ex.Message);
            }
            try
            {
                k = Convert.ToInt32(textBox2.Text);

                mas1 = new float[n, k];
                if (k > m)
                {
                    for (int i = 0; i < n; ++i)
                        for (int j = 0; j < m; ++j)
                            mas1[i, j] = mas[i, j];
                }
                else
                {
                    dataGridView1.ColumnCount = k;
                    for (int i = 0; i < n; ++i)
                        for (int j = 0; j < k; ++j)
                            mas1[i, j] = mas[i, j];
                }
                m = k;
                mas = mas1;
                dataGridView1.ColumnCount = k;
            }
            catch (Exception ex) 
            {
                k = 3;
                MessageBox.Show(ex.Message); 
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int k;
            float[,] mas1;
            try
            {
                k = Convert.ToInt32(textBox3.Text);
                mas1 = new float[k, m1];
                if (k > n1)
                {
                    for (int i = 0; i < n1; ++i)
                        for (int j = 0; j < m1; ++j)
                            mas1[i, j] = mas2[i, j];
                }
                else
                {
                    dataGridView2.RowCount = k;
                    for (int i = 0; i < k; ++i)
                        for (int j = 0; j < m1; ++j)
                            mas1[i, j] = mas2[i, j];
                }
                n1 = k;
                mas2 = mas1;
                dataGridView2.RowCount = k;
            }
            catch (Exception ex)
            {
                k = 3;
                MessageBox.Show(ex.Message);
            }
            try
            {
                k = Convert.ToInt32(textBox4.Text);
                mas1 = new float[n1, k];
                if (k > m1)
                {
                    for (int i = 0; i < n1; ++i)
                        for (int j = 0; j < m1; ++j)
                            mas1[i, j] = mas2[i, j];
                }
                else
                {
                    dataGridView2.ColumnCount = k;
                    for (int i = 0; i < n1; ++i)
                        for (int j = 0; j < k; ++j)
                            mas1[i, j] = mas2[i, j];
                }
                m1 = k;
                mas2 = mas1;
                dataGridView2.ColumnCount = k;
            }
            catch (Exception ex)
            {
                k = 3;
                MessageBox.Show(ex.Message);
            }
        }
    }
}
